import {
  login,
  logout,
  getInfo
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    id: '',
    name: '',
    avatar: '',
    phone: '',
    realName: '',
    email: '',
    status: '',
    loginType: '',
    permission: {},
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token // VueX存储token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },
  SET_REALNAME: (state, realName) => {
    state.realName = realName
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_STATUS: (state, status) => {
    state.status = status
  },
  SET_LOGIN_TYPE: (state, loginType) => {
    state.loginType = loginType
  },
  SET_ROLE: (state, role) => {
    state.role = role
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_PERMISSION: (state, permission) => {
    state.permission = permission
  },
}

const actions = {
  // user login
  login({
          commit
        }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password,
        validateCodeId: userInfo.validateCodeId,
        validateCode: userInfo.validateCode
      }).then(response => {
        const {
          result
        } = response
        console.log(result)
        commit('SET_TOKEN', result.token) // VueX存储token
        // commit('SET_PERMISSION', result.permission) //VueX存储permission
        setToken(result.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({
            commit,
            state
          }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const {
          result
        } = response

        if (!result) {
          return reject('Verification failed, please Login again.')
        }

        const {
          id,
          username,
          realName,
          avatar,
          phone,
          email,
          status,
          loginType,
          role
        } = result

        commit('SET_NAME', username)
        commit('SET_AVATAR', avatar)
        commit('SET_PHONE', phone)
        commit('SET_REALNAME', realName)
        commit('SET_EMAIL', email)
        commit('SET_STATUS', status)
        commit('SET_LOGIN_TYPE', loginType)
        commit('SET_ROLE', role)
        commit('SET_ID', id)
        resolve(result)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({
           commit,
           state
         }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
               commit
             }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
